//
//  ByCellMyOrder.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/20.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellMyOrder.h"

@implementation ByCellMyOrder

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    // 我的订单
    UILabel *myOrderLabel = [[UILabel alloc] init];
    myOrderLabel.adjustsFontSizeToFitWidth = true;
    myOrderLabel.numberOfLines = 1;
    myOrderLabel.text = @"我的订单";
    myOrderLabel.font = FONT(16);
    myOrderLabel.textAlignment = NSTextAlignmentLeft;
    myOrderLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:myOrderLabel];
    [myOrderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.width.mas_equalTo([myOrderLabel.text getSizeOfString:16].width + 10);
        make.top.equalTo(self);
        make.height.equalTo(@36);
    }];
    
    // 分隔线
    UIView *line = [UIView new];
    line.backgroundColor = BY_LINE_COLOR;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(myOrderLabel.mas_bottom);
        make.height.equalTo(@1);
    }];
    
    // 查看全部（按钮+文字）
    UIControl *lookAllView = [UIControl new];
    [self.contentView addSubview:lookAllView];
    [lookAllView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(myOrderLabel);
        make.height.equalTo(myOrderLabel);
        make.right.equalTo(self.contentView).offset(-5);
        make.left.equalTo(myOrderLabel.mas_right);
    }];
    UIImageView *arrowView = [UIImageView new];
    arrowView.image = [UIImage imageNamed:@"右箭头"];
    [lookAllView addSubview:arrowView];
    [arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lookAllView);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(lookAllView);
    }];
    UILabel *lookNoteLabel = [[UILabel alloc] init];
    lookNoteLabel.adjustsFontSizeToFitWidth = true;
    lookNoteLabel.numberOfLines = 1;
    lookNoteLabel.text = @"查看全部";
    lookNoteLabel.font = FONT(14);
    lookNoteLabel.textAlignment = NSTextAlignmentRight;
    lookNoteLabel.textColor = [UIColor lightGrayColor];
    [lookAllView addSubview:lookNoteLabel];
    [lookNoteLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(arrowView.mas_left).offset(-5);
        make.left.top.height.equalTo(lookAllView);
    }];
    
    // 其他订单（待取货，待发货，待收货，待评价，已退款）
    NSArray *titles = @[@"待取货", @"待发货", @"待收货", @"待评价", @"已退款"];
    for (int i = 0; i < titles.count; i ++) {
        UIView *basalView = [UIView new];
        basalView.tag = i + 10;
        [self.contentView addSubview:basalView];
        [basalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.contentView).multipliedBy(0.2);
            make.top.equalTo(line.mas_bottom);
            make.bottom.equalTo(self.contentView);
            make.centerX.equalTo(self.contentView).multipliedBy(0.2 * (i * 2 + 1));
        }];
    
        UILabel *noteLabel = [[UILabel alloc] init];
        noteLabel.adjustsFontSizeToFitWidth = true;
        noteLabel.numberOfLines = 1;
        noteLabel.text = titles[i];
        noteLabel.font = FONT(14);
        noteLabel.textAlignment = NSTextAlignmentCenter;
        noteLabel.textColor = [UIColor blackColor];
        [basalView addSubview:noteLabel];
        [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(basalView);
            make.bottom.equalTo(basalView).offset(-10);
            make.height.equalTo(@20);
        }];
        
        UIImageView *iconView = [UIImageView new];
        iconView.backgroundColor = [UIColor orangeColor];
        [basalView addSubview:iconView];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(40, 40));
            make.centerX.equalTo(basalView);
            make.bottom.equalTo(noteLabel.mas_top).offset(-10);
        }];
        
        UILabel *badgeLabel = [[UILabel alloc] init];
        badgeLabel.tag = 100;
        badgeLabel.backgroundColor = BY_RED_COLOR;
        badgeLabel.font = FONT(10);
        badgeLabel.textAlignment = NSTextAlignmentCenter;
        badgeLabel.textColor = [UIColor whiteColor];
        badgeLabel.layer.cornerRadius = 10;
        badgeLabel.layer.masksToBounds = true;
        [basalView addSubview:badgeLabel];
        [badgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(-5));
            make.top.equalTo(@5);
            make.height.equalTo(@20);
            make.width.greaterThanOrEqualTo(@20);
        }];
    }
}


- (void)setOrderBadges:(NSArray *)orderBadges {
    
    _orderBadges = orderBadges;
    
    NSInteger baseTag = 10;
    for (int i = 0; i < orderBadges.count; i ++) {
        UIView *basalView = [self.contentView viewWithTag:baseTag + i];
        UILabel *badgeLabel = (UILabel *)[basalView viewWithTag:100];
        NSInteger badge = [orderBadges[i] integerValue];
        if (badge == 0) {
            badgeLabel.hidden = true;
        } else {
            badgeLabel.hidden = false;
            badgeLabel.text = [NSString stringWithFormat:@"%li", badge];
        }
    }
}

#pragma mark - 点击事件
// 查看全部订单
- (void)lookAllOrder {
    
    
}


@end
